package cn.hlife.com.hlfhaemocyte.systemsetting.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.hlife.com.hlfhaemocyte.MainActivity;
import cn.hlife.com.hlfhaemocyte.R;
import cn.hlife.com.hlfhaemocyte.utils.FragmentUtils;
import cn.hlife.com.hlfhaemocyte.utils.SharedPreferencesUtils;

/**
 * Created by Administrator on 2017/8/1.
 */

public class Char2Setting extends Fragment implements View.OnClickListener {


    @BindView(R.id.rbc_et)
    EditText rbcEt;
    @BindView(R.id.wbc_et)
    EditText wbcEt;
    @BindView(R.id.plt_et)
    EditText pltEt;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.char2_setting, container, false);
        ButterKnife.bind(this, view);
        ((MainActivity) getActivity()).middleText.setText("预稀释直方图设置");
        ((MainActivity) getActivity()).rightText.setOnClickListener(this);
        ((MainActivity) getActivity()).rightText.setText("保存");
        ((MainActivity) getActivity()).rightText.setVisibility(View.VISIBLE);
        ((MainActivity) getActivity()).returnImageView.setVisibility(View.VISIBLE);
        ((MainActivity) getActivity()).returnImageView.setOnClickListener(this);
        initView();
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            ((MainActivity) getActivity()).middleText.setText("预稀释直方图设置");
            ((MainActivity) getActivity()).rightText.setOnClickListener(this);
            ((MainActivity) getActivity()).rightText.setText("保存");
            ((MainActivity) getActivity()).rightText.setVisibility(View.VISIBLE);
            ((MainActivity) getActivity()).returnImageView.setVisibility(View.VISIBLE);
            ((MainActivity) getActivity()).returnImageView.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.left_text:
                save();
                break;
            case  R.id.return_image:
                ((MainActivity)getActivity()).currentFragment = FragmentUtils.switchFragment
                        (getFragmentManager(), R.id.fragment_content, ((MainActivity)getActivity()).currentFragment, SettingFragment.class, null);
                break;
        }
    }
    public  void  initView(){
        String jsonData=  SharedPreferencesUtils.getInstance().getYXSRefernce(getContext());
        try {
            JSONObject   map=new JSONObject(jsonData);
            pltEt.setText(TextUtils.isEmpty(map.getString("PLT"))?"":map.getString("PLT"));
            rbcEt.setText(TextUtils.isEmpty(map.getString("RBC"))?"":map.getString("RBC"));
            wbcEt.setText(TextUtils.isEmpty(map.getString("WBC"))?"":map.getString("WBC"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public void  save(){
        JSONObject  jsonObject=new JSONObject();
        try {
            jsonObject.put("PLT",pltEt.getText().toString());
            jsonObject.put("RBC",rbcEt.getText().toString());
            jsonObject.put("WBC",wbcEt.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Toast.makeText(getContext(),"数据已保存",Toast.LENGTH_SHORT).show();
        SharedPreferencesUtils.getInstance().saveYXSRefernce(getContext(),jsonObject.toString());
        ((MainActivity)getActivity()).currentFragment = FragmentUtils.switchFragment
                (getFragmentManager(), R.id.fragment_content, ((MainActivity)getActivity()).currentFragment, SettingFragment.class, null);
    }
}
